//
//  FKAddressCell.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/1/28.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKAddressCell.h"

@interface FKAddressCell ()

@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *mobileLabel;
@property (nonatomic, weak) UILabel *addressLabel;
@property (nonatomic, weak) UIButton *setDefaultBtn;
@property (nonatomic, weak) UIButton *deleteBtn;
@property (nonatomic, weak) UIButton *editBtn;

@end

@implementation FKAddressCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UILabel *nameLabel = [UILabel fk_labelWithFont:fk_adjustFont(15) textColor:fkColor333333];
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UILabel *mobileLabel = [UILabel fk_labelWithFont:fk_adjustFont(15) textColor:fkColor333333 textAlignment:NSTextAlignmentRight];
    [self.contentView addSubview:mobileLabel];
    self.mobileLabel = mobileLabel;
    
    UILabel *addressLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [self.contentView addSubview:addressLabel];
    self.addressLabel = addressLabel;
    addressLabel.numberOfLines = 0;
    
    UIView *bottomView = [[UIView alloc] init];
    [self.contentView addSubview:bottomView];
    
    UIButton *setDefaultBtn = [UIButton fk_btnWithTarget:self action:@selector(setDefaultBtnClicked) font:fk_adjustFont(13) normalTitle:@"设为默认" normalTitleColor:fkColor999999 selectedTitle:@"默认地址" selectedTitleColor:fkMainColor normalImg:[UIImage imageNamed:@"bank_icon_select_n"] selectedImg:[UIImage imageNamed:@"bank_icon_select_s"] bgImg:nil selectedBgImg:nil];
    [bottomView addSubview:setDefaultBtn];
    self.setDefaultBtn = setDefaultBtn;
    setDefaultBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
    setDefaultBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
    UIButton *deleteBtn = [UIButton fk_btnWithTarget:self action:@selector(deleteBtnClicked) font:fk_adjustFont(13) normalTitle:@"删除" normalTitleColor:fkColor666666 selectedTitle:nil selectedTitleColor:nil normalImg:[UIImage imageNamed:@"address_address_delete"] selectedImg:nil bgImg:nil selectedBgImg:nil];
    [bottomView addSubview:deleteBtn];
    self.deleteBtn = deleteBtn;
    deleteBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    deleteBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    UIButton *editBtn = [UIButton fk_btnWithTarget:self action:@selector(editBtnClicked) font:fk_adjustFont(13) normalTitle:@"编辑" normalTitleColor:fkColor666666 selectedTitle:nil selectedTitleColor:nil normalImg:[UIImage imageNamed:@"address_address_editor"] selectedImg:nil bgImg:nil selectedBgImg:nil];
    [bottomView addSubview:editBtn];
    self.editBtn = editBtn;
    editBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    editBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    UIView *separator = [[UIView alloc] init];
    [bottomView addSubview:separator];
    separator.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.top.offset(fk_adjustW(14));
    }];
    [mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-12);
        make.top.equalTo(nameLabel);
    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.bottom.offset(0);
        make.height.mas_equalTo(fk_adjustW(40));
    }];
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(0.8);
    }];
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.right.equalTo(mobileLabel);
        make.bottom.equalTo(bottomView.mas_top).offset(fk_adjustW(-16));
    }];
    [setDefaultBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.offset(0);
        make.width.mas_equalTo(100);
    }];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.offset(0);
        make.width.mas_equalTo(fk_adjustW(54));
    }];
    [editBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(deleteBtn.mas_left).offset(fk_adjustW(-24));
        make.top.bottom.width.equalTo(deleteBtn);
    }];
}

- (void)setDefaultBtnClicked
{
    if ([self.delegate respondsToSelector:@selector(addressCell:setDefaultBtnClicked:)]) {
        [self.delegate addressCell:self setDefaultBtnClicked:self.data[@"id"]];
    }
}

- (void)deleteBtnClicked
{
    if ([self.delegate respondsToSelector:@selector(addressCell:deleteBtnClicked:)]) {
        [self.delegate addressCell:self deleteBtnClicked:self.data[@"id"]];
    }
}

- (void)editBtnClicked
{
    if ([self.delegate respondsToSelector:@selector(addressCell:editBtnClicked:)]) {
        [self.delegate addressCell:self editBtnClicked:self.data];
    }
}

- (void)setData:(id)data
{
    [super setData:data];
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        self.nameLabel.text = data[@"shperson"];
        self.mobileLabel.text = data[@"mobile"];
        self.addressLabel.text = fkFORMAT(@"%@%@%@%@", data[@"province"], data[@"city"], data[@"county"], data[@"detail_info"]);
        self.setDefaultBtn.selected = [data[@"status"] isEqualToString:@"1"];
    }
}

@end
